<?php
class ContributeAction extends BaseAction{
	function _initialize(){
		parent::_initialize();
		set_time_limit(0);
	}

	function _outExcel($dataArr, $fileName='contribute'){
/*		require ADDON_PATH.'/libs/phpExcel/php-excel.class.php';	
		
		$xls = new Excel_XML('UTF-8');
		$xls->addArray($dataArr);
		$xls->generateXML($fileName);	*/

        require ADDON_PATH.'/libs/phpExcel/out-csv.php';
        export_csv($dataArr);
        
        unset($dataArr);
	}
		
	public function index(){
		$this->display();
	}
	
	function outUser(){
		addLog($this->mid, '', 'out_user');
		
		$dataArr = array(
		1 => array ('人员信息', '', '积分', '', '知道', '', '', '论坛', '', '', '百科', '', '博客', '', '时间范围'),
		2 => array ('姓名', '工号', '贡献', '金币', '提交', '回答', '被采纳', '发帖', '精华', '回帖', '创建', '编辑', '文章', '排名', '起始日期', '截止日期')
		);	
		
		$field = array('fullname', 'uname', 'credit_exp', 'credit_score', 
		               'askcount', 'answercount', 'typecount', 'topiccount', 'distcount', 'postcount', 'wikiaddcount', 'wikieditcount', 
		               'blogIdcount', 'userOrder');	
		
		$data = $this->_getUserData();
		
		$dataArr = $this->_dealDataArr($data, $field, $dataArr, 3, true);

		$this->_outExcel($dataArr, "user_".date('Y-m-d'));
	}
	//集中处理SQL查询并把结果合并到以用户ID为索引的数据里
	function _merge_data($data=array(), $sql='', $field=array()){
		$list = D('')->query($sql);
		foreach ($list as $v){			
			foreach ($field as $vo){
				$data[ $v['uid'] ][$vo] = $v[$vo];
			}			
		}

		return $data;	
	}
	//提取查询出来的数据里的用户ID，用户下一步获取用户信息
	function _getDataUid($data){
		foreach ($data as $k=>$v){			
			$uid .= $k.',';			
		}	
		
		return rtrim($uid, ',');	
	}

	function _getUserData(){
		//知道提交数
		$sql ="SELECT b.uid,  COUNT( b.askId ) AS askcount 
FROM  ts_ask AS b WHERE b.uid!=0 ".$this->_whereTime('cTime')." GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('askcount'));
		
		//知道回答数/采纳数
		$sql = "SELECT b.uid, SUM(b.type) AS typecount,  COUNT( b.answerId ) AS answercount
FROM  ts_ask_answer AS b WHERE b.uid!=0 ".$this->_whereTime('cTime')." GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('typecount', 'answercount'));

		//用户发帖数/精华数
		$sql = "SELECT b.uid, COUNT( b.id ) AS topiccount, sum(b.dist) as distcount  
FROM  ts_bbs_topic AS b WHERE b.isdel=0 ".$this->_whereTime('cTime')." GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('topiccount', 'distcount'));	

		//用户回帖数
		$sql = "SELECT b.uid,  COUNT( b.id ) AS postcount 
FROM  ts_bbs_post AS b WHERE b.istopic = 0 AND b.isdel=0 ".$this->_whereTime('cTime')."  GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('postcount'));

		//百科创建数
		$sql = "SELECT b.authorId as uid,  COUNT( b.id ) AS wikiaddcount 
FROM  ts_wiki AS b WHERE b.recycleId = 0 ".$this->_whereTime('createTime')."  GROUP BY uid";
		$data = $this->_merge_data($data, $sql, array('wikiaddcount'));
		
		//百科编辑数
		$sql = "SELECT b.lastEditorId as uid, COUNT( b.id ) AS wikieditcount 
FROM ts_wiki_history AS b WHERE b.recycleId = 0 ".$this->_whereTime('lastEditTime')." GROUP BY uid";
		$data = $this->_merge_data($data, $sql, array('wikieditcount'));

		//博客数
		$sql = "SELECT b.uid,  COUNT( b.blogId ) AS blogIdcount 
FROM  ts_blog AS b WHERE b.status IN(1,2) ".$this->_whereTime('cTime')."  GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('blogIdcount'));
		
		//博客排名
		$sql = "SELECT uid  FROM ts_blog_mention GROUP BY uid order by userOrder desc";
		$list = D('')->query($sql);
		foreach ($list as $k=>$v){
			$data[ $v['uid'] ]['userOrder'] = $k+1;
		}	
		
		//用户名，工号，部门
		$uid = $this->_getDataUid($data);
		$data = $this->_getUserInfo($data, $uid, false);		
		
		return $data;		
	}
	
	function outUserCate(){
		addLog($this->mid, '', 'out_cate');
		
		$dataArr = array(
		1 => array ('', '', '知道数据:', '', '','论坛数据:', '', '', '百科数据:', '', '时间范围'),
		2 => array ('姓名', '工号', '提交', '回答', '被采纳', '发帖', '精华', '回帖', '创建', '编辑', '起始日期', '截止日期')
		);	
		
		if(!empty($_POST['ask_Id'])){
			$cate = $_POST['ask_Id'];
			$data = $this->_getAskUser($cate);
		}
		if(!empty($_POST['bbs_Id'])){
			$cate = $_POST['bbs_Id'];
			$data = $this->_getBbsUser($cate, $data);
		}
		if(!empty($_POST['wiki_Id'])){
			$cate = $_POST['wiki_Id'];
			$data = $this->_getWikiUser($cate, $data);
		}
		
		//用户名，工号，部门
		$uid = $this->_getDataUid($data);
		$data = $this->_getUserInfo($data, $uid, false);			

		$field = array('fullname', 'uname',	'askcount', 'answercount', 'typecount', 'topiccount', 'distcount', 'postcount', 'wikiaddcount','wikieditcount');
		$dataArr = $this->_dealDataArr($data, $field, $dataArr, 1, true);

		$this->_outExcel($dataArr, "userApp_".$_POST['app']."_".date('Y-m-d'));
	}
		
	function _getAskUser($cate){		
		$sql = "SELECT `askId` FROM `ts_ask` WHERE ( `categoryList` LIKE '$cate%' )";
		$list = D('')->query($sql);
		$askId = $this->_getArrId($list, 'askId');
				
		//知道提交数
		$sql = "SELECT b.uid, COUNT( b.askId ) AS askcount 
FROM ts_ask AS b WHERE b.askId IN( $askId ) ".$this->_whereTime('cTime')." GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('askcount'));
		
		//知道回答数/采纳数
		$sql = "SELECT b.uid, SUM(b.type) AS typecount,  COUNT( b.answerId ) AS answercount
FROM  ts_ask_answer AS b WHERE b.askId IN( $askId ) ".$this->_whereTime('cTime')." GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('typecount', 'answercount'));
		
		return $data;
	}
		
	function _getBbsUser($cate, $data){					
		$map['board'] = array('like', "$cate%");
		$list = D('Topic', 'bbs')->where($map)->field('id')->findall();
		$bbsId = $this->_getArrId($list, 'id');

		//用户发帖数
		$sql = "SELECT b.uid, COUNT( b.id ) AS topiccount, sum(b.dist) as distcount 
FROM  ts_bbs_topic AS b WHERE b.id IN( $bbsId ) AND b.isdel=0 ".$this->_whereTime('cTime')." GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('topiccount', 'distcount'));
		
		//用户回帖数
		$sql = "SELECT b.uid,  COUNT( b.id ) AS postcount 
FROM  ts_bbs_post AS b WHERE b.istopic = 0 AND b.isdel=0 AND b.tid IN( $bbsId ) ".$this->_whereTime('cTime')."  GROUP BY b.uid";
		$data = $this->_merge_data($data, $sql, array('postcount'));

		return $data;
	}
		
	function _getWikiUser($cate, $data){
		$map['categoryId'] = $cate;
		$map['recycleId']  = 0;
		$wiki = D('WikiCategory', 'wiki')->where($map)->field('wikiId')->findall();
		$wikiId = $this->_getArrId($wiki, 'wikiId');
		
		//百科创建数
		$sql = "SELECT b.authorId as uid,  COUNT( b.id ) AS wikiaddcount 
FROM  ts_wiki AS b WHERE b.recycleId = 0 AND b.id IN( $wikiId ) ".$this->_whereTime('createTime')."  GROUP BY uid";
		$data = $this->_merge_data($data, $sql, array('wikiaddcount'));
		
		//百科编辑数
		$sql = "SELECT b.lastEditorId as uid, COUNT( b.id ) AS wikieditcount 
FROM ts_wiki_history AS b WHERE b.recycleId = 0 AND b.wikiId IN( $wikiId ) ".$this->_whereTime('lastEditTime')." GROUP BY uid";
        $data = $this->_merge_data($data, $sql, array('wikieditcount'));
					
		return $data;	
	}
	
	function _whereTime($field, $f='b'){		
		$where = '';
		$from = strtotime($_POST['from']);
		$to   = strtotime($_POST['to']);
		
		if(!empty($from) && !empty($to)){
			$where = " AND $f.$field BETWEEN $from AND $to";
		}elseif(!empty($from) && empty($to)){
			$where = " AND $f.$field > $from";
		}elseif(empty($from) && !empty($to)){
			$where = " AND $f.$field < $to";
		}
		
		return $where;
	}

	function _getArrId($list, $field){
		foreach ($list as $v){
			$id[] = $v[$field];
		}
		if(empty($id)) $id = 0;
		else           $id = implode(',', $id);	
		
		return $id;	
	}
	
	function outAppCate(){
		addLog($this->mid, array('app'=>$_POST['app']), 'out_app');
		
		switch ($_POST['app']){
			case 'bbs':	 $dataArr = $this->getBbsCate();	break;
			case 'wiki': $dataArr = $this->getWikiCate();	break;
			case 'blog': $dataArr = $this->getBlogCate();	break;
			case 'group':$dataArr = $this->getGroupCate();	break;
			default:     $dataArr = $this->getAskCate();    //ask
		}

		$this->_outExcel($dataArr, "appCate_".$_POST['app']."_".date('Y-m-d'));
	}
	
	function getAskCate(){
		$dataArr = array(
		1 => array ('分类信息', '', '问题', '', '', '', '', '', '回答', '', '专家管理员', '', '', '', '时间范围', ''),
		2 => array ('一级分类', '二级分类', '总数', '待解决', '已解决', '关闭', '过期', '零回复', '回答数', '被采纳', '专家姓名', '专家工号', '管理员姓名', '管理员工号', '起始日期', '截止日期')
		);
		
		$data = $this->_getAskCateData();//dump($data);exit;
		$field = array('cate1', 'cate2', 'askcount', 'count_0', 'count_1', 'count_2', 'count_3', 'noAnswerCount', 'answercount', 'acceptcount', 'expert', 'expert_uname', 'admin', 'admin_uname');	
		foreach ($data as $v){
			foreach ($field as $k=>$vo){
				$arr[$k] = $v[$vo];
			}
			$arr[$k+1] = $_POST['from'];
			$arr[$k+2] = $_POST['to'];

			$dataArr[] = $arr;
			unset($arr);
		}

		return $dataArr;
	}
	
	function _getAskCateData(){  	
		$sql = "SELECT b.askId, COUNT(b.answerId) AS answercount, SUM(b.type) AS acceptcount 
FROM  ts_ask_answer AS b 
WHERE b.askId <>'' ".$this->_whereTime('cTime','b')." GROUP BY b.askId";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$askId = $v['askId'];
			unset($v['askId']);
			
			$ask[$askId] = $v;
		}

		$sql = "SELECT a.categoryList, a.askId
FROM ts_ask AS a
WHERE a.categoryList <>'' ".$this->_whereTime('cTime', 'a');
		$list = D('')->query($sql);
		foreach ($list as $v){
			$c = $v['categoryList'];
			$askId = $v['askId'];
			if(empty($c)) continue;
			
			if(isset($arr[$c])){
				$arr[$c]['answercount'] += $ask[$askId]['answercount'];
				$arr[$c]['acceptcount'] += $ask[$askId]['acceptcount'];
				$arr[$c]['askcount'] += 1;
			}else {
				$arr[$c] = $ask[$askId];
				$arr[$c]['askcount'] = 1;
			}			
		}
		unset($sql);
		

        $sql[] = "SELECT a.categoryList, COUNT(a.askId) AS noAnswerCount 
FROM ts_ask AS a
WHERE a.answerCount=0 AND a.status=0 ".$this->_whereTime('cTime', 'a').
" GROUP BY a.categoryList";
        for($i=0; $i<4; $i++){
        	$sql[] = "SELECT a.categoryList, COUNT(a.askId) AS count_$i
FROM ts_ask AS a
where a.status=$i ".$this->_whereTime('cTime', 'a').
" GROUP BY a.categoryList";
        }
        
        //合并查询结果
		foreach ($sql as $s){//dump($v);
			$list = D('')->query($s);
			
			foreach ($list as $k=>$v){
				$key = $v['categoryList'];
				
				if(isset($arr[$key])){
					$arr[$key] =  array_merge($arr[$key], $v);
				}else{
					$arr[$key] = $v;
				}
			}
		} 

		$field = array('askcount','count_0','count_1','count_2','count_3','noAnswerCount','answercount','acceptcount');		
		//组装二级数据
		foreach ($arr as $v){
			$cateArr = explode(',', $v['categoryList']);
			$pid  = intval($cateArr[0]);
			$id  = intval($cateArr[1]);
			$result[$pid.$id] =  $v;
		}
	
		//取分类信息
  		$data = model('Xcate')->getTree('ask',$cid);
  		$data = $this->_dealTreeData($data, 1);
  		foreach ($data as &$v){
  			$id = $v['id'];
  			$pid = $v['pid'];
  			unset($v['id']);  			
  			
  			if(!isset($v['cate2'])){//组装一级数据
  				foreach ($v['d'] as $vv){
  					foreach ($field as $vo){
  						$count[$vo] += $result[$id.$vv][$vo];
  					}
  				}
  				
  				$v = array_merge($v, $count);
  				unset($count);
  			}elseif(!empty($result[$pid.$id])){//二级数据
				$v = array_merge($v, $result[$pid.$id]);
			}
			foreach ($field as $vo){
				if(!isset($v[$vo])){
					$v[$vo] = 0;
				}
			}			
		}		

		return $data;	
	}
	
	function _dealTreeData($data, $i=1, $pid=0, $pTitle=''){
		foreach ($data as $k=>$v){			
			$id = $v['a'];
			$e  = $this->_getMultiUserInfo( $v['e'] ); 
			$m  = $this->_getMultiUserInfo( $v['m'] );
			
			$arr[$id]['id'] = $id;
			if($i==2){
				$arr[$id]['cate1'] = $pTitle;
			}
			$arr[$id]['cate'.$i] = $v['t'];
			
			$arr[$id]['pid'] = $pid;
			$arr[$id]['expert_uname'] = $e['uname'];
			$arr[$id]['admin_uname'] = $m['uname'];		
			$arr[$id]['expert'] = $e['name'];
			$arr[$id]['admin'] = $m['name'];			

			if(!empty($v['d']) && $i<2){
				$res = $this->_dealTreeData($v['d'], $i+1, $id, $v['t']);
				$arr[$id]['d'] = getSubByKey($res, 'id');
				
				$arr = array_merge($arr, $res);
			}
		}

		return $arr;
	}
			
	function getBbsCate(){		
		$dataArr = array(
		1 => array ('分类信息', '', '', '帖子信息', '', '', '', '管理员', '', '时间范围', ''),
		2 => array ('一级分类', '二级分类', '三级分类', '主题', '精华', '回复', '浏览', '管理员姓名', '管理员工号', '起始日期', '截止日期')
		);		
		
		$data = $this->_getBbsCateData();
		$field = array('cate1', 'cate2', 'cate3', 'topiccount', 'distcount', 'postcount', 'viewcount', 'banzhu', 'banzhu_uname');
		foreach ($data as $v){
			foreach ($field as $k=>$vo){
				$arr[$k] = $v[$vo];
			}
			$arr[$k+1] = $_POST['from'];
			$arr[$k+2] = $_POST['to'];			
			
			$dataArr[] = $arr;
			unset($arr);
		}

		return $dataArr;	
	}
	
	function _getBbsCateData(){	
		$sql = "SELECT a.tagid, count(a.id) as topiccount, sum(a.dist) as distcount, sum(a.viewcount) as viewcount FROM ts_bbs_topic as a
		WHERE a.isdel=0 ".$this->_whereTime('cTime', 'a')." GROUP BY a.tagid";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$result[ $v['tagid'] ] = $v;
		}
	
		$sql = "SELECT a.tagid, count(b.id) as postcount 
		FROM ts_bbs_topic as a
		LEFT JOIN ts_bbs_post as b ON a.id=b.tid 
		WHERE b.istopic = 0 AND b.isdel=0 ".$this->_whereTime('cTime', 'b')." GROUP BY a.tagid";
		$list = D('')->query($sql);
		foreach ($list as $v){
			if(!empty($v['tagid'])){
				$result[ $v['tagid'] ]['postcount'] = $v['postcount'];
			}			
		}
		
		$sql ="SELECT tagid, banzhu FROM ts_bbs";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$banzhu  = $this->_getMultiUserInfo( $v['banzhu'] );
			$result[ $v['tagid'] ]['banzhu'] = $banzhu['name'];
			$result[ $v['tagid'] ]['banzhu_uname'] = $banzhu['uname'];
		}	

		$tree = D('Core', 'bbs')->getCategory();
		
		$list = $this->_bbsTree($tree, $result);
		$field = array ('topiccount','postcount','distcount','viewcount');		
		foreach ($list as $k=>&$v){
			foreach ($field as $vo){
				if(!isset($v[$vo])){
					$v[$vo] = 0;
				}
			}
		}
		
		return $list;
	}
	//整合等级树
	function _bbsTree($tree, $result, $leven=1){
		static $_cate;
		$cate = "cate".$leven;
		
		foreach ($tree as $v){
			$id =  $v['a'];
			$res = $result[$id];
			
			$_cate[$leven] = $v['t'];
			for($i=1; $i<$leven; $i++){
				$res['cate'.$i]  =  $_cate[$i];
			}
			$res['cate'.$leven]  =  $v['t'];
			
			$list[] = $res;
			
			if(!empty($v['d'])){
				$back = $this->_bbsTree($v['d'], $result, $leven+1);
				if(!empty($back)){
					$i = count($list)-1;				
					
					$field = array ('topiccount','postcount','distcount','viewcount');
					foreach ($back as $b){
						foreach ($field as $vo){
							$list[$i][$vo] += $b[$vo];
						}
					}
					
					$list = array_merge($list, $back);
				}
			}
		}
		
		return $list;
	}
	
	function getGroupCate(){
		$dataArr = array(
		1 => array ('基本信息', '', '', '创建人', '', '管理员', '', '数据信息', '', '', '', '', '', '', '', '时间范围', ''),
		2 => array ('分类', '团队名称', '团队ID', '创建时期', '姓名', '工号', '管理员姓名', '管理员工号', '成员数', '主题数', '回复数', '词条数', '文档数', '空间大小', '博文数', '浏览数', '起始日期', '截止日期')
		);
		
		$data = $this->_getGroupCateData();
		$field = array('category', 'name', 'gid', 'cTime', 'fullname', 'uname', 'admin', 'admin_uname', 'member', 'bbscount', 'postcount', 'wikicount', 'filecount', 'filesize', 'blogcount', 'visitcount');
		return $this->_dealDataArr($data, $field, $dataArr, 6, true);
	}
	
	function _getGroupCateData(){
		//取全部分类
		$sql = "SELECT id,name FROM ts_group_x_category WHERE status=1";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$cata[$v['id']] = $v['name'];
		}
		
		$sql = "SELECT gid,level,uid FROM ts_group_x_member WHERE status=1";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$gid = $v['gid'];
			if($v['level']>1){
				$u = $this->_getUserInfo('', $v['uid']);
				$member[$gid]['admin'] .= $u['fullname'].',';
				$member[$gid]['admin_uname'] .= $u['uname'].',';
			}
			$member[$gid]['membercount'] += 1;
		}

		//取全部团队status=1
		$sql = "SELECT gid, uid, name, category, visitcount, cTime FROM ts_group_x WHERE status=1";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$gid = $v['gid'];
			$u = $this->_getUserInfo('', $v['uid']);
			
			$v['gid'] = $gid;
			$v['fullname'] = $u['fullname'];
			$v['uname']    = $u['uname'];			
			$v['category'] = $cata[$v['category']];
			$v['cTime']    = date("Y-m-d", $v['cTime']);
			$v['admin']    = rtrim($member[$gid]['admin'], ',');
			$v['admin_uname']    = rtrim($member[$gid]['admin_uname'], ',');
			$v['member']   = $member[$gid]['membercount'];

			$res[$gid] = $v;
		}

		$sql = "SELECT b.gid,count(b.id) as bbscount
FROM ts_group_forum_topic as b 
WHERE b.isdel=0 ".
$this->_whereTime('cTime', 'b').
" GROUP BY b.gid";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$gid = $v['gid'];
			
			if(isset($res[$gid])) {
				$res[$gid]['bbscount'] = $v['bbscount'];
			}
		}

		$sql = "SELECT c.gid, count(c.id) as postcount
FROM ts_group_forum_post as c 
WHERE c.istopic=0 AND c.isdel=0 ".
$this->_whereTime('cTime', 'c').
" GROUP BY c.gid";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$gid = $v['gid'];
			
			if(isset($res[$gid])) {
				$res[$gid]['postcount'] = $v['postcount'];
			}
		}
		
		$sql = "SELECT d.groupId, count(d.id) as wikicount
FROM ts_group_wiki as d 
WHERE d.activeWikiId=d.id AND recycleId=0 ".
$this->_whereTime('createTime', 'd').
" GROUP BY d.groupId";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$gid = $v['groupId'];
			
			if(isset($res[$gid])) {
				$res[$gid]['wikicount'] = $v['wikicount'];
			}
		}

		$sql = "SELECT e.gid,count(e.id) as filecount, sum(e.filesize) as filesize 
FROM ts_group_file as e 
WHERE e.attachType = 'file' AND e.isDel=0 ".
$this->_whereTime('cTime', 'e').
" GROUP BY e.gid";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$gid = $v['gid'];
			
			if(isset($res[$gid])) {
				$res[$gid]['filecount'] = $v['filecount'];
				$res[$gid]['filesize']  = round($v['filesize']/1048576, 2);
			}
		}

		$sql = "SELECT f.gid, count(f.id) as blogcount
FROM ts_group_blog as f 
LEFT JOIN ts_blog as a ON f.blogId=a.blogId 
WHERE f.gid<>'' ".
$this->_whereTime('cTime', 'a').
" GROUP BY f.gid";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$gid = $v['gid'];
			
			if(isset($res[$gid])) {
				$res[$gid]['blogcount'] = $v['blogcount'];
			}
		}
		
		return $res;
	}
	
	function getBlogCate(){
		$dataArr = array(
		1 => array ('博主信息', '', '分类信息', '', '博文信息', '', '', '', '', '时间范围'),
		2 => array ('姓名', '工号', '系统分类', '个人分类', '标题', 'Digg', '回复', '浏览', '发布日期', '起始日期', '截止日期')
		);
		
		$data = $this->_getBlogCateData();
		foreach ($data as $v){			
			$arr = array($v['name'],$v['uname'],$v['cate1'],$v['cate2'],$v['title'],$v['digCount'],$v['commentCount'],$v['readCount'],$v['cTime'], $_POST['from'], $_POST['to']);
			
			$dataArr[] = $arr;
		}
		
		return $dataArr;		
	}
	
	function _getBlogCateData(){
		//取全部分类
		$sql = "SELECT id,name,pid FROM ts_blog_category WHERE type='user'";
		$list = D('')->query($sql);
		foreach ($list as $v){
			$cata[$v['id']]  = $v['name'];
		}
		foreach ($list as $v){
			if($v['pid']!=0){
				$pcata[$v['id']]  = $cata[$v['pid']];
			}
		}

		//取全部博客列表
		$sql = "SELECT b.uid, b.name, b.title, b.category, b.readCount, b.digCount, b.commentCount, b.cTime 
FROM ts_blog AS b
WHERE b.status IN(1,2) ".$this->_whereTime('cTime', 'b').
" ORDER BY b.uid ASC, b.category asc";

		$list = D('')->query($sql);
		foreach ($list as &$v){
			$v['cate1'] = $pcata[$v['category']];
			$v['cate2'] = $cata[$v['category']];
			$v['uname'] = $this->getUserUname($v['uid']);
			$v['cTime'] = date("Y-m-d", $v['cTime']);		
		}

		return $list;
			
	}
	
	function getUserUname($uid){
		$_uname;
		
		if(empty($_uname[$uid])){
			$_uname[$uid] = M('User')->where("uid=$uid")->getField('uname');
		}
		
		return $_uname[$uid];
	}
		
	function getWikiCate(){		
		$dataArr = array(
		1 => array ('分类信息', '', '', '词条信息','','时间范围'),
		2 => array ('一级分类', '二级分类', '三级分类', '词条数', '编辑数', '起始日期', '截止日期')
		);		
		
		$data = $this->_getWikiCateData();
		foreach ($data as $v){			
			$arr = array (
			   $v['cate1'],
			   $v['cate2'],
			   $v['cate3'],
			   $v['wikiaddcount'],
			   $v['wikieditcount'],
			   $_POST['from'],
			   $_POST['to']
			);
			
			$dataArr[] = $arr;
		}

		return $dataArr;	
	}
	
	function makeTreeData($list,$pid='0'){
		foreach ($list as $k=>$v){
			if($v['pid']==$pid){
				$id = $v['id'];
				
				if(empty($v['pid'])){
					$npid = $v['id'];
				}else{
					$npid = $v['pid'].','.$v['id'];
				}
				
				$data[$id]['id'] = $v['id'];
				$data[$id]['name'] = $v['name'];

				$data[$id]['d'] = $this->makeTreeData($list, $npid);
			}
		}
		
		return $data;
	}
	function _getWikiCateData(){	
		//取全部分类
		$sql = "SELECT id,name,level,navigation FROM ts_wiki_category";
		$list = D('')->query($sql);
		foreach ($list as &$v){
			$navigation = unserialize($v['navigation']);
			unset($v['navigation']);
			
			for($i=1; $i<$v['level']; $i++){
				$j = $i-1;
				$pid[] = $navigation[$j]['cid'];
			}
			
			$v['pid'] = implode(',', $pid);
			if(empty($v['pid'])){
				$v['pid'] = '0';
			}
			unset($pid);		
		}	
		
		$cate = $this->makeTreeData($list);

		//取全部列表
		$sql = "SELECT c.id, count(a.wikiId) as wikiaddcount, sum(b.edits) as wikieditcount 
FROM ts_wiki AS b
LEFT JOIN ts_wiki_category_link AS a ON a.wikiId = b.id AND b.recycleId=0 ".$this->_whereTime('createTime', 'b').
" LEFT JOIN ts_wiki_category AS c ON c.id = a.categoryId 
GROUP BY c.id";

        $list = D('')->query($sql);
        foreach ($list as $v){
        	$id = $v['id'];
        	unset($v['id']);
        	
        	$arr[$id] = $v;
        }
        
        return $this->_dealWikiData($cate, $arr, 1);
	}	
	
	function _dealWikiData($cate, $arr, $level=1){
		static $_cate;
		foreach ($cate as $v){
			$data = $arr[$v['id']];
			if(empty($data)){
				$data['wikiaddcount'] = 0;
				$data['wikieditcount'] = 0;
			}
			
			$_cate[$level] = $v['name'];
			for($i=1; $i<$level;$i++){
				$data['cate'.$i] = $_cate[$i];
			}
			
			$data['cate'.$level] = $v['name'];
			
			$list[] = $data;
			if(!empty($v['d'])){
				$back = $this->_dealWikiData($v['d'], $arr, $level+1);
				if(!empty($back)){
					$list = array_merge($list, $back);
				}
			}
		}
		
		return $list;
	}
	
	function _getUserInfo($data='', $uid='', $isOne=true){
		static $_user;
		if($isOne && isset($_user[ $uid ])){
			return $_user[ $uid ];
		}
		//用户ID，工号
		$sql = "SELECT uid, cardId as uname, credit_score, credit_exp FROM ts_space WHERE uid IS NOT NULL";
		if(!empty($uid)){
			$sql .= " AND uid IN($uid)";
		}
		$list = D('')->query($sql);
		foreach ($list as $v){
			$data[ $v['uid'] ]['uname'] = $v['uname'];
			$data[ $v['uid'] ]['credit_score'] = $v['credit_score'];
			$data[ $v['uid'] ]['credit_exp'] = $v['credit_exp'];
		}
		//用户名
		$sql = "SELECT uid, value FROM ts_space_search WHERE info='username'";
		if(!empty($uid)){
			$sql .= " AND uid IN($uid)";
		}		
		$list = D('')->query($sql);
		foreach ($list as $v){
			if(isset( $data[ $v['uid'] ] )){
				$data[ $v['uid'] ]['fullname'] = $v['value'];
			}
			
			if($isOne){
				return $_user[ $uid ] = $data[ $v['uid'] ];
			}
		}	
		
		return $data;	
	}
	
	function _dealDataArr($data, $field, $dataArr, $maxK=2, $showTime=false){
		foreach ($data as $i=>$v){
			$count = 0;
			
			foreach ($field as $k=>$vo){
				if($k>$maxK){					
					if(empty($v[$vo])){
						$v[$vo] = 0;
					}	
					
					$count += $v[$vo];				
				}
				
				$arr[$k] = $v[$vo]; 
			}
			
			if($count==0){
				unset($data[$i]);
				continue;
			}
			
			if($showTime){
				$arr[$k+1] = $_POST['from']; 
				$arr[$k+2] = $_POST['to'];
			}

			$dataArr[] = $arr;
			unset($arr);
		}

		return 	$dataArr;
	}
	
	function _getMultiUserInfo($userList){
		$userList = explode(',', $userList);
		foreach ($userList as $k=>$v){
			if(empty($v)) continue;
			
			$user = $this->_getUserInfo('', $v);
			
			$name[$k] = $user['fullname'];
			$uname[$k] = $user['uname'];
		}
		
		$res['name'] = implode(',', $name);
		$res['uname'] = implode(',', $uname);
		
		return $res;
	}
}
?>